import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbclass, tbcourseassignment, tbjob, tbjobpublish } from '../../../../atoms/db';

const logger = anylogger('pages/api/zllweapp/zuoyepiyue/s001.api');

export type Idata = Pick<ITbcourseassignment, 'arrangementtime' | 'coursename' | 'jobtitle' | 'jobcontent' | 'teachername'>;

export type Result = {
	ok: true;
	data: Idata;
	count: number;
	data1: Pick<ITbclass, 'classname' | 'majorname' | 'class_id'>[]
} | {
	ok: false;
	message: string;
};


export type Message = {
	id: string;
}

/**
 * pages/api/zllweapp/zuoyepiyue/s001.api
 */
const handler = an48<Result>();
handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { id } = req.body as Message;

		const [{ num }] = await tbjob().count('approvedlogo', { as: 'num' }).where({
			approvedlogo: 2
		});
		const count = Number(num);

		const data = await tbcourseassignment().first('jobtitle', 'jobcontent', 'arrangementtime', 'coursename', 'teachername').where({
			job_id: id
		});
		const class_id = await tbjobpublish().select('class_id').where({
			job_id: id
		});
		const class_id1 = class_id.map((it) => {
			return it.class_id;
		});
		const data1 = await tbclass().select('classname', 'majorname', 'class_id').whereIn(
			'class_id', class_id1
		);

		res.status(200).json({ ok: true, count, data, data1 });


	} catch (error) {
		logger.error(error);
		res.status(200).json({
			ok: false,
			message: '失败'
		});
	}
});

export const config = {} as PageConfig;

export default handler;
